package cn.edu.xmu.oomall.customer.controller;

import cn.edu.xmu.javaee.core.aop.Audit;
import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.javaee.core.validation.NewGroup;
import cn.edu.xmu.oomall.customer.controller.dto.AddressDto;
import cn.edu.xmu.oomall.customer.controller.vo.AddressVo;
import cn.edu.xmu.oomall.customer.dao.bo.Address;
import cn.edu.xmu.oomall.customer.service.AddressService;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.edu.xmu.javaee.core.util.CloneFactory;

import java.util.List;

/**
 * @author 谢传艳
 * @date 2024/12/19
 */
@RestController
@RequestMapping(value = "/addresses", produces = "application/json;charset=UTF-8")
@RequiredArgsConstructor
@Transactional
public class AddressController {
    private final AddressService addressService;

    /**
     * 添加地址
     * @param addressDto 地址信息
     * @param user 用户
     */
    @Audit(departName = "addresses")
    @PostMapping
    public ReturnObject addAddress(@Validated(NewGroup.class) @RequestBody AddressDto addressDto,
                                   @LoginUser UserDto user){
        Address address = CloneFactory.copy(new Address(), addressDto);
        address = addressService.addAddress(address, user);
        return new ReturnObject(ReturnNo.CREATED, CloneFactory.copy(new AddressVo(address), address));
    }

    /**
     * 查询用户所有地址
     */
    @Audit(departName = "addresses")
    @GetMapping
    public ReturnObject getAllAddress(@LoginUser UserDto user){
        List<Address> addressList = addressService.findAddressesByCustomerId(user.getId());
        return new ReturnObject(ReturnNo.OK, addressList.stream().map(o -> CloneFactory.copy(new AddressVo(o), o)).toList());
    }

    /**
     * 设置默认地址
     * @param id 地址id
     */
    @Audit(departName = "addresses")
    @PutMapping("/{id}/default")
    public ReturnObject setDefaultAddress(@PathVariable Long id,
                                          @LoginUser UserDto user){
        addressService.setDefaultAddress(id, user);
        return new ReturnObject(ReturnNo.OK);
    }

    /**
     * 修改地址信息
     * @param id 地址id
     */
    @Audit(departName = "addresses")
    @PutMapping("/{id}")
    public ReturnObject modifyAddressInfo(@PathVariable Long id,
                                      @RequestBody AddressDto addressDto,
                                      @LoginUser UserDto user){
        Address address = CloneFactory.copy(new Address(), addressDto);
        address.setId(id);
        addressService.modifyAddress(address, user);
        return new ReturnObject(ReturnNo.OK);
    }

    /**
     * 删除地址
     * @param id 地址id
     */
    @Audit(departName = "addresses")
    @DeleteMapping("/{id}")
    public ReturnObject delAddress(@PathVariable Long id){
        addressService.deleteAddress(id);
        return new ReturnObject(ReturnNo.OK);
    }
}
